package com.centerserver;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Calendar;
import java.util.Map;

import javax.swing.DefaultListModel;

class RecvSpsPpsThread extends Thread {
	
	private boolean mIsFinish = false;
	private InputStream inputstream;
	private Socket mSocket;
	private String mIpAddress;
	private FileOutputStream mFileOutputStream;
	private String mFileFullPath;
	private Map<String, ProcessRtpPacketThread> mThreadMap;
	private DatabaseManager mDatabaseManager;
	
	public RecvSpsPpsThread(Socket socket, DatabaseManager databaseManager, 
			Map<String, ProcessRtpPacketThread> threadMap,
			DefaultListModel captureVideoItemListModel) {
		
		mSocket = socket;
		mThreadMap = threadMap;
		mDatabaseManager = databaseManager;
		mIpAddress = ((InetSocketAddress)mSocket.getRemoteSocketAddress()).getAddress().getHostAddress();
		
		String captureEndName = databaseManager.getCaptureEndName(mIpAddress);
		String rootDirectory = CenterServerConfig.RECORD_VIDEO_FULL_PATH_ROOT_DIR.concat(captureEndName).concat("\\");
		
		// get the current date & time-range
		Calendar rightNow = Calendar.getInstance();
		int curYear = rightNow.get(Calendar.YEAR);
		int curMonth = rightNow.get(Calendar.MONTH)+1;
		int curDay = rightNow.get(Calendar.DAY_OF_MONTH);;
		int curTimeRange =  rightNow.get(Calendar.HOUR_OF_DAY);
		String videoName = String.format("%4d%02d%02d%02d.264", curYear, curMonth, curDay, curTimeRange);
//		System.out.println(videoName);
		
		mFileFullPath = rootDirectory.concat(videoName);
		mDatabaseManager.addCaptureVideoItem(mIpAddress, videoName.substring(0, 10));
		captureVideoItemListModel.addElement(videoName.substring(0, 10));
		
		try {
			mFileOutputStream = new FileOutputStream(mFileFullPath, true);
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException in RecvSpsPpsThread");
			e.printStackTrace();
		}
		
		try {
			inputstream = socket.getInputStream();
		} catch (IOException e) {
			System.out.println("IOException in ReceiveSpsPps");
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		
		int recvPacketNum = 0;
		while(!mIsFinish) {

			// accept the client's request
			try {
				byte spsPpsBuf[] = new byte[CenterServerConfig.SPS_OR_PPS_SIZE];
				byte btSpsPpsLen[] = new byte[2]; 

				inputstream.read(btSpsPpsLen, 0, 2);

				int highBit =  btSpsPpsLen[0]<0 ? 256 + btSpsPpsLen[0] : btSpsPpsLen[0];
				int lowBit = btSpsPpsLen[1]<0 ? 256 + btSpsPpsLen[1] : btSpsPpsLen[1];
				int spsPpsLen = (highBit<<8) + lowBit;

				int bufLen = inputstream.read(spsPpsBuf, 0, spsPpsLen);
				
				if( bufLen > 0 ) {
					recvPacketNum++;
					mFileOutputStream.write(spsPpsBuf, 0, spsPpsLen);
					
					// save SPS 
					if( 1 == recvPacketNum) {
						String sps = new String(spsPpsBuf, 0, spsPpsLen, "ISO-8859-1");
						mDatabaseManager.saveSps(mIpAddress, sps);
					}
					
					// SPS & PPS received
					if( 2 == recvPacketNum ) {	
						
						// save PPS
						String pps = new String(spsPpsBuf, 0, spsPpsLen, "ISO-8859-1");
						mDatabaseManager.savePps(mIpAddress, pps);
						
						mThreadMap.get(mIpAddress).setFileOutputStream(mFileOutputStream);
						break;
					}
				}

			}
			catch(IOException e) {
				System.out.println("IOException in RecvSpsPpsThread");
				//e.printStackTrace();
			}
		}
	}
	
	// set the finish flag
	public void setIsFinish(boolean isFinish) {
		
		this.mIsFinish = isFinish;
	}
} 